package com.brillilab.service.core.plans.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.plans.PlanExperimentGroupMapper;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.plans.PlanExperimentGroup;
import com.brillilab.domain.po.topics.TopicExperiment;
import com.brillilab.service.core.plans.IPlanExperimentGroupService;
import com.brillilab.service.core.plans.IPlanService;

/**
 * <p>
 * 计划实验目标组表 服务实现类
 * </p>
 *
 * @author zlb
 * @since 2019-09-05
 */
@Service
@Transactional
public class PlanExperimentGroupServiceImpl implements IPlanExperimentGroupService {

	@Resource
	private PlanExperimentGroupMapper planExperimentGroupMapper;
	@Resource
	private IPlanService planService;
	
	@Override
	public List<PlanExperimentGroup> getExperimentGroupList(Long targetGroupId) {
		return planExperimentGroupMapper.selectList(
				new LambdaQueryWrapper<PlanExperimentGroup>().eq(PlanExperimentGroup::getTargetGroupId, targetGroupId)
						.eq(PlanExperimentGroup::getIsDelete, BoolEnum.FALSE.getValue()));
	}

	@Override
	public PlanExperimentGroup insert(PlanExperimentGroup experimentGroup) {
		int insert = planExperimentGroupMapper.insert(experimentGroup);
		Assert.isTrue(insert == 1, "添加失败");
		return experimentGroup;
	}

	@Override
	public PlanExperimentGroup getExperimentGroup(Long targetGroupId, Long topicExperimentId) {
		return planExperimentGroupMapper.selectOne(
				new LambdaQueryWrapper<PlanExperimentGroup>()
				.eq(PlanExperimentGroup::getTargetGroupId, targetGroupId)
						.eq(PlanExperimentGroup::getTopicExperimentId, topicExperimentId));
	}

	@Override
	public void deleteById(Long experimentGroupId) {
		int i = planExperimentGroupMapper.deleteById(experimentGroupId);
		Assert.isTrue(i == 1, "删除失败");
	}

	@Override
	public PlanExperimentGroup selectById(Long experimentGroupId) {
		return planExperimentGroupMapper.selectById(experimentGroupId);
	}

	@Override
	public void updateById(PlanExperimentGroup experimentGroup) {
		int i = planExperimentGroupMapper.updateById(experimentGroup);
		Assert.isTrue(i == 1, "更新失败");
	}

	@Override
	public void delete(Long topicExperimentId) {
		planExperimentGroupMapper.delete(new LambdaQueryWrapper<PlanExperimentGroup>()
				.eq(PlanExperimentGroup::getTopicExperimentId, topicExperimentId));
		planService.deleteByTopicExperimentId(topicExperimentId);
	}

	@Override
	public void deleteByTargetGroupId(Long targetGroupId) {
		planExperimentGroupMapper.delete(new LambdaQueryWrapper<PlanExperimentGroup>()
				.eq(PlanExperimentGroup::getTargetGroupId, targetGroupId));
	}

	@Override
	public List<TopicExperiment> getTopicExperimentList(Long targetGroupId, Integer isCheck) {
		return planExperimentGroupMapper.getTopicExperimentList(targetGroupId,isCheck);
	}

}
